package com.qiantu.phone.utils;


import android.content.Intent;

import androidx.lifecycle.LifecycleOwner;

import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.db.LLSceneDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.DeviceCustomKeyBean;
import com.qiantu.api.entity.SceneDetailBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.event.SceneChangeEvent;
import com.qiantu.phone.ui.activity.EditSceneActivityNew;
import com.qiantu.phone.ui.activity.EditSceneIntentActivity;
import com.qiantu.phone.ui.activity.EditScenePrivateActivity;
import com.qiantu.phone.ui.uiutil.DeviceType;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Call;

public class SceneDetailUtil {

    public static void getSceneDetailAndToEdit(LifecycleOwner lifecycleOwner, AppActivity appActivity, String sceneSerialNo) {
        getSceneDetailAndToEdit(lifecycleOwner,appActivity,sceneSerialNo,null);
    }


    public static void getSceneDetailAndToEdit(LifecycleOwner lifecycleOwner, AppActivity appActivity, String sceneSerialNo,String targetSerialNo){
        LLHttpManager.getSceneDetailInfo(lifecycleOwner, sceneSerialNo, new HttpCallback<HttpData<List<SceneDetailBean>>>(null) {

            @Override
            public void onStart(Call call) {
                super.onStart(call);
                if (appActivity != null) {
                    appActivity.showDialog();
                }
            }

            @Override
            public void onSucceed(HttpData<List<SceneDetailBean>> result) {
                super.onSucceed(result);
                Observable.create(new ObservableOnSubscribe<SceneDetailBean>() {
                            @Override
                            public void subscribe(ObservableEmitter<SceneDetailBean> emitter) throws Exception {
                                SceneDetailBean data = result.getData().get(0);
                                LLSceneDBManager.getInstance(appActivity).update(data);
                                //获取服务器后的场景详情，更新下本地数据，避免场景列表和详情页数据显示不一致
                                EventBus.getDefault().post(new SceneChangeEvent(data.getSceneSerialNo()));
                                TempDataHolder.setEditSceneDetailBean(data);
                                emitter.onNext(data);
                            }
                        }).subscribeOn(Schedulers.computation())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<SceneDetailBean>() {
                            @Override
                            public void accept(SceneDetailBean data) throws Exception {
                                getDeviceCustomKeyListAndToSceneEdit(data, lifecycleOwner, appActivity,targetSerialNo);
                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                            }
                        });
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
                if (appActivity != null) {
                    appActivity.hideDialog();
                }
            }
        });
    }

    public static void getDeviceCustomKeyListAndToSceneEdit(SceneDetailBean data, LifecycleOwner lifecycleOwner, AppActivity appActivity, String targetSerialNo){
//        Observable.create(new ObservableOnSubscribe<SceneDetailBean>() {
//            @Override
//            public void subscribe(ObservableEmitter<SceneDetailBean> emitter) throws Exception {
//                List<String> deviceSerialNos = new ArrayList<>();
//                if (data.getSceneTasks() != null) {
//                    for (SceneDetailBean.SceneTaskBean sceneTask : data.getSceneTasks()) {
//                        if (sceneTask.getSceneDeviceInstruct() != null) {
//                            DeviceBean device = LLDeviceDBManager.getInstance(AppApplication.getInstance()).get(sceneTask.getSceneDeviceInstruct().getDeviceItemSerialNo());
//                            if (device != null && (device.getDeviceType().equals(DeviceType.INFRARED_KEY_3.stringType)
//                                    || device.getDeviceType().equals(DeviceType.INFRARED_KEY_9.stringType)
//                                    || device.getDeviceType().equals(DeviceType.WIRELESS_315_KEY_3.stringType)
//                                    || device.getDeviceType().equals(DeviceType.WIRELESS_315_KEY_9.stringType)
//                                    || device.getDeviceType().equals(DeviceType.WIRELESS_433_KEY_3.stringType)
//                                    || device.getDeviceType().equals(DeviceType.WIRELESS_433_KEY_9.stringType))) {
//                                deviceSerialNos.add(device.getDeviceSerialNo());
//                            }
//                        }
//                    }
//                }
//                //有自定义按键名称的设备，就需要先获取下自定义名称列表，防止任务里的按钮没有名称
//                if (deviceSerialNos.size() > 0) {
//                    LLHttpManager.getDeviceCustomKeyList(lifecycleOwner, deviceSerialNos, new HttpCallback<HttpData<DeviceCustomKeyBean>>(null) {
//                        @Override
//                        public void onSucceed(HttpData<DeviceCustomKeyBean> result) {
//                            List<DeviceCustomKeyBean.KeyData> customKeyList = result.getData().getDeviceCustomKeys();
//                            LLDeviceDBManager.getInstance(appActivity).updateDevicesCustomKeys(deviceSerialNos, customKeyList);
//                            emitter.onNext(data);
//                        }
//
//                        @Override
//                        public void onFail(Exception e) {
//                            emitter.onNext(data);
//                        }
//                    });
//                } else {
//                    emitter.onNext(data);
//                }
//            }
//        }).subscribeOn(Schedulers.computation())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Consumer<SceneDetailBean>() {
//                    @Override
//                    public void accept(SceneDetailBean result) throws Exception {
//                        toSceneEdit(appActivity, result);
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                    }
//                });
        toSceneEdit(appActivity, data,targetSerialNo);
    }

    public static void toSceneEdit(AppActivity appActivity, SceneDetailBean data, String targetSerialNo){
        if (appActivity != null) {
            appActivity.hideDialog();
            if(data != null){
                switch (data.getSceneType()) {
                    case 1:
                        Intent intent = new Intent(appActivity,EditSceneActivityNew.class);
                        intent.putExtra("targetSerialNo",targetSerialNo);
                        appActivity.startActivity(intent);
                        break;
                    case 3:
                        appActivity.startActivity(EditSceneIntentActivity.class);
                        break;
                    case 4:
                        appActivity.startActivity(EditScenePrivateActivity.class);
                        break;
                }
            }
        }
    }
}
